package com.lancoo.edu.platform.operation.commons.service.api;

import com.alibaba.fastjson.TypeReference;
import com.lancoo.edu.platform.operation.commons.domain.api.app.AppIdentity;
import com.lancoo.edu.platform.operation.commons.domain.api.app.ApplicationInfo;
import com.lancoo.edu.platform.operation.commons.domain.api.code.Code3Info;
import com.lancoo.edu.platform.operation.commons.domain.api.system.ServerInfo;
import com.lancoo.edu.platform.operation.commons.utils.EncryptUtil;
import com.lancoo.edu.platform.operation.commons.utils.HttpUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * {校园应用资源管理主站点}API
 * @author yjk
 */
@Service
public class ApplicationInfoApiService extends YunBaseApiService{
    /**
     * 校园应用资源管理的系统ID
     */
    private static final String APPLICATION_SYS_ID = "Y01";

    /**
     * 获取用户身份下的应用列表
     * @param userId 用户ID
     * @param userType 用户类型
     * @param identityCode 身份代码
     * @param eduId 教育局ID
     * @return 身份列表
     */
    public List<AppIdentity> getAppByIdentity(String userId, int userType, String identityCode, String eduId){
        ServerInfo serverInfo = getOneServerInfo(APPLICATION_SYS_ID);
        String url = serverInfo.getWebSvrAddr() + "/api/AppResMgr/DeskMgr/GetAllModuleInfoForSearch";
        Map<String, String> paramMap = getParamMap();
        paramMap.put("UserID", userId);
        paramMap.put("UserType", userType+"");
        paramMap.put("IdentityCode", identityCode);
        paramMap.put("SchoolID", eduId);
        paramMap.put("SecCode", EncryptUtil.MD5(eduId));
        String resultStr = HttpUtil.sendGet(url, paramMap);
        return getData3List(resultStr,new TypeReference<Code3Info<List<AppIdentity>>>(){});
    }

    /**
     * 获取所有应用
     * @param eduId 教育局ID
     * @return 应用列表
     */
    public List<ApplicationInfo> getAllApplication(String eduId){
        ServerInfo info = getOneServerInfo(APPLICATION_SYS_ID);
        String url = info.getWebSvrAddr() + "/api/AppResMgr/AppMgr/GetAllAppGroupAndAppInfoForWorkPlatform";
        Map<String, String> paramsMap = getParamMap();
        paramsMap.put("SchoolID", eduId);
        paramsMap.put("SecCode", EncryptUtil.MD5(eduId));
        String resultStr = HttpUtil.sendGet(url, paramsMap);
        // 设置json 反序列化类型，并提取data数据返回
        return getData3List(resultStr,new TypeReference<Code3Info<List<ApplicationInfo>>>(){});
    }
}
